package ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.event.MenuListener;
import javax.swing.event.MenuEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.util.Date;

import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JButton;

import repository.IRepositories;

import domain.Task;

import app.AppInstance;

public class MainFrm {

	private JFrame frame;
	private JTextField taskName;
	private JTextArea taskDescription; 
	private JTextField taskTitle;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrm window = new MainFrm();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainFrm() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 727, 461);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnfile = new JMenu("File");
		mnfile.setMnemonic('F');
		menuBar.add(mnfile);
		
		JMenuItem mnExit = new JMenuItem("Exit");
		mnExit.setMnemonic('x');
		mnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//JOptionPane.showMessageDialog(null, "Salir");
				System.exit(0);
			}
		});

		mnfile.add(mnExit);
		frame.getContentPane().setLayout(null);
		
		taskName = new JTextField();
		taskName.setBounds(115, 33, 346, 20);
		frame.getContentPane().add(taskName);
		taskName.setColumns(10);
		
		JLabel lblTaskName = new JLabel("Task Name");
		lblTaskName.setBounds(42, 36, 76, 14);
		frame.getContentPane().add(lblTaskName);
				
		taskDescription = new JTextArea();
		taskDescription.setBounds(1, 1, 135, 29);
		frame.getContentPane().add(taskDescription);
		
		JScrollPane scpTaskDescription = new JScrollPane(taskDescription);
		scpTaskDescription.setBounds(42, 97, 419, 200);
		frame.getContentPane().add(scpTaskDescription);
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveTask();
			}
		});
		btnSave.setBounds(63, 345, 89, 23);
		frame.getContentPane().add(btnSave);
		
		JLabel lblTaskTitle = new JLabel("Task Title");
		lblTaskTitle.setBounds(42, 61, 76, 14);
		frame.getContentPane().add(lblTaskTitle);
		
		taskTitle = new JTextField();
		taskTitle.setColumns(10);
		taskTitle.setBounds(115, 61, 346, 20);
		frame.getContentPane().add(taskTitle);
	}
	
	private void saveTask(){
		
		IRepositories rep = AppInstance.getInstance().getRepositories();
		Task t = rep.getRepositoriesFactory().createTask();
		t.setName(taskName.getText());
		t.setTitle(taskTitle.getText());
		t.setDescription(taskDescription.getText());
		t.setStartTime(new Date());
		t.setEndTime(new Date());
			
		rep.getTaskRepository().saveTask(t);
	}
}
